/**
 * function:
 * 
		**
		 * name:	'name',
		 * value:	'value',
		 * type:	'type',
		 * attr:	{
		 * 		label:		'label',
		 * 		defval:		'value',
		 * 		editable:	true,
		 * 		nullable:	true,
		 * 		pk:			false,
		 * 		signed:		false,//for num
		 * 		lenght:		25,
		 * 		comment:	'comment',
		 * 		css:		'class',
		 * 		range:		'0#:#male#;#1#:#female'//for options
		 * }
		 *
 * requrement:jquery.core, jquery.form, util
 * @author watson  email: watson.wu@hotmail.com
 * @date 2009-9-2
 */
$.fn.MagicForm=function(opt){
	var cfg={
		edit:false,
		url:false,//submit action of form
		target:'ajax',//extends methods of form, like:'ajax', 'ajax-get', 'post', 'get', 'blank', and etc.
		closeAfterSubmit:false,
		callbackAfterSubmit:false,
		listyle:false,
		data:false//fields definition
	};
	opt=$.extend(true,{},cfg,opt);
	var $this=$(this);
	if(!validate(opt.data))	return ;
	var $form=$("<form class='magic-form' />").attr({id:'editor-',method:(opt.target && opt.target=='get')?'get':'post', action:opt.url});
	var $html=$('<ul/>');
	for(var i in opt.data){
		if(opt.data[i].attrs.pk && !opt.edit)
			continue;
		$html.append($("<li id='li"+i+"' "+(opt.data[i].attrs.pk?'class="hd"':'')+(opt.listyle?"class='"+opt.listyle+"'":"")+"/>").append(opt.data[i].attrs.html?opt.data[i].attrs.html:complie(opt.data[i])));
	}
	if(opt.edit=='query'){
		if($form.find('li').length>0)
			$form.append($html.append('<li id="li-bts-inl"'+(opt.listyle?"class='"+opt.listyle+"'":"")+'><input type="submit"/><input type="reset"/></li>')).appendTo($('#'+$this.attr('id')));
	}else
		$form.append($html.append('<li id="li-bts"'+(opt.listyle?"class='"+opt.listyle+"'":"")+'><input type="submit"/><input type="reset"/></li>')).appendTo($('#'+$this.attr('id')));
	if(opt.target=='ajax'||opt.target=='ajax-get'){
		$(document).ready(function(){
			$form.ajaxForm({
				success:function(json){
					if(opt.callbackAfterSubmit)
						opt.callbackAfterSubmit.call();
					if(opt.closeAfterSubmit){
						
					}
					if(json.flag)
						showMessage('success',null);
			}});
		});
	}

	function validate(data) {
		//TODO
		return true;
	};
	function complie(data) {
		var $html=new StringBuffer('<label id="lable');
		$html.append(data.name).append('">').append(data.attrs.nullable?'':'<span class="required">*</span>').append(data.attrs.label?data.attrs.label:data.name).append('</label>');
		switch(data.type){
		case 'id':
			$html.append('<input id="ed_').append(data.name).append('" name="').append(data.name).append('" value="').append(data.value? data.value:data.attrs.defval?data.attrs.defval:"").append('"').append(data.attrs.css?" class='"+data.attrs.css+"'":"").append(' type="text" size="30" scope="id" disabled="disabled"').append(data.attrs.comment?' title="'+data.attrs.comment+'"':'').append(opt.edit?" style='display:none;'":"").append(' />');
			break;
		case 'num':
			$html.append('<input id="ed_').append(data.name).append('" name="').append(data.name).append('" value="').append(data.value? data.value:data.attrs.defval?data.attrs.defval:"").append('"').append(data.attrs.css?" class='"+data.attrs.css+"'":"").append(' type="text" size="30" scope="num').append((data.attrs.signed?' signed':'')+(data.attrs.nullable?'':' required')+(data.attrs.reg?' '+data.attrs.reg:' ')).append('"').append(data.attrs.editable? "":" disabled='disabled'").append(data.attrs.pk?" style='display:none;'":"").append(data.attrs.comment?' title="'+data.attrs.comment+'"':'').append(data.attrs.length? " length='"+data.attrs.length+"'":"").append(' />');
			break;
		case 'string':
			if('text'==data.attrs.type){
				$html.append('<textarea id="ed_').append(data.name).append('" name="').append(data.name).append('"').append(' scope="').append((data.attrs.nullable?'':'required')+(data.attrs.reg?' '+data.attrs.reg:'')).append('"').append(data.attrs.editable? "":" disabled='disabled'").append(data.attrs.comment?' title="'+data.attrs.comment+'"':'').append(' cols="80" rows="').append(data.attrs.length? (data.attrs.length/80):50).append('">').append(data.value? data.value:data.attrs.defval?data.attrs.defval:"").append('</textarea>');
			} else if(data.attrs.type == 'rich') {
//				$html;
			} else
				$html.append('<input id="ed_').append(data.name).append('" name="').append(data.name).append('" value="').append(data.value? data.value:data.attrs.defval?data.attrs.defval:"").append('"').append(data.attrs.css?" class='"+data.attrs.css+"'":"").append(' type="text" size="30" scope="').append((data.attrs.nullable?'':'required')+(data.attrs.reg?' '+data.attrs.reg:' ')).append('"').append(data.attrs.editable? "":" disabled='disabled'").append(data.attrs.pk?" style='display:none;'":"").append(data.attrs.comment?' title="'+data.attrs.comment+'"':'').append(data.attrs.length? " length='"+data.attrs.length+"'":"").append(' />');
			break;
		case 'option': 
			if('radio'==data.attrs.type){
				var $tmp=new StringBuffer('<input type="radio" id="ed_');
				$temp.append(data.name).append('" name="').append(data.name).append('" ');
				var list = data.attrs.range.split('#;#');
				for(var i=0;i<list.length;i++) {
					var kv = list[i].split("#:#");
					$html.append(tmp.append(' value="').append(kv[1]).append('" />&nbsp;').append(kv[0]));
				}
			} else {
				$html.append('<select id="ed_').append(data.name).append('" name="').append(data.name).append('" >');
				var list = data.attrs.range.split('#;#');
				for(var i=0;i<list.length;i++) {
					var kv = list[i].split("#:#");
					$html.append("<option value='").append(kv[1]).append("'>").append(kv[0]).append("</option>");
				}
				$html.append("</select>");
			}
			break;
		case 'image': 
			$html.append('<img src="').append(data.value? data.value:data.attrs.defval?data.attrs.defval:"../image/nopohoto.gif").append('" /><input id="ed_').append(data.name).append('" name="').append(data.name).append('" value="').append(data.value? data.value:data.attrs.defval?data.attrs.defval:"").append('"').append(data.attrs.css?" class='"+data.attrs.css+"'":"").append(' type="text" size="30" scope="').append((data.attrs.nullable?'':'required')+(data.attrs.reg?' '+data.attrs.reg:'')).append('"').append(data.attrs.editable? "":" disabled='disabled'").append(data.attrs.comment?' title="'+data.attrs.comment+'"':'').append(data.attrs.length? " length='"+data.attrs.length+"'":"").append(' />');
			break;
		case 'password': 
			break;
		case 'date': 
			break;
		default: break;
		}
		return $html.toString();
	};
};